*********************************************************************
***      IDENTIFY EMERGENCY VISITS IN INPATIENT OUTPATIENT FILES  ***
*** Goes through all revenue files and claims files 			  ***
*** and identifies claims associated with an         			  ***
*** emergency department visit. FFS patients only			      ***
*** Also identifies whether there was an ambulance claim assoc w  ***
*** the claim in outpatient file or 20% carrier file.  			  ***
*** Last edited: 1/20/2023                       				  ***
*********************************************************************

log using "${CodePath}/log/identifyemergency_$S_DATE.log", text replace

// Switches
local op_indivyear 		 = 1
local ipop_indivyear 	 = 1
local ipop_collapsed 	 = 1
local combine_ptoutcomes = 1
local ptoutcomes_fy 	 = 1


*--------------------------------------------------
* Identify emergency visits in the outpatient file
*--------------------------------------------------
if `op_indivyear' == 1{
	*--------------------------------------------------
	* Identifying emergency visits in outpatient revenue files:  Revenue Center Codes 0450-0459, 0981, then merge in info from claims file
	* count the number of unique emergency department dates as a proxy for an ED visit. https://resdac.org/cms-data/variables/hospital-outpatient-emergency-room-visits
	* rev center 0450, 0451, 0452, 0456, 0459
	* also counts whether there was an ambulance revenue code associated with this claim
	* note that this only identifies ambulance rides where the hospital owns the ambulance. this only covers about 5% of all rides!
	* have to then find the rest of the ambulance rides in the carrier file
	*--------------------------------------------------
	// from claim info (linked via claim_id)
		// beneficiary id
		// admitting diagnosis
		// provider
		// admission and discharge date
		// total charges
		// year
		// race, zip, sex
		// death within a year
		// chronic conditions

	forval year = 2006/2015{
	*forval year = 2012/2015{
		di "Outpatient - `year'"
		*preserve
	 		use clm_id bene_id rev_cntr using "${McareDataPath}/harm/100pct/op/`year'/opr`year'.dta", clear
			di "destringing rev_cntr..."
			gen nonnum = real(rev_cntr) == .
			tab nonnum
			drop if nonnum
			drop nonnum

			destring rev_cntr, replace

			di "identifying ambulance..."
			gen ambulance_temp = rev_cntr >= 540 & rev_cntr <= 549
			bys clm_id: egen ambulance = max(ambulance_temp)

			di "restricting to just ED visits..."
			keep if (rev_cntr >= 450 & rev_cntr <= 459) | rev_cntr == 981

			di "dropping clm_id duplicates..."
			duplicates drop clm_id, force
			keep clm_id bene_id ambulance 
			gen file_year = `year' 

			di "merging in bsfab..."
			merge m:1 bene_id using "${McareDataPath}/harm/100pct/bsfab/`year'/bsfab`year'.dta", keepusing(bene_dob sex race g_bene_zip5 death_dt hmoind*)
			tab _merge 
			keep if _merge == 3 | _merge == 1
			drop _merge 	

			di "merging in claim info..."
			preserve
				use clm_id provider icd_dgns_cd1 from_dt thru_dt tot_chrg pmt_amt prvdrpmt using "${McareDataPath}/harm/100pct/op/`year'/opc`year'.dta", clear
				rename provider prvdrnum
				keep if clm_id != ""
				duplicates drop clm_id, force
				tempfile temp
				save `temp'
			restore

			merge m:1 clm_id using `temp'
			tab _merge 
			keep if _merge == 3 | _merge == 1
			drop _merge

			di "keeping only ffs patient"
			// Keep only if FFS patient in that month
			gen disc_mon = month(thru_dt)
			keep if (disc_mon == 1 & hmoind01 == "0" | disc_mon == 2 & hmoind02 == "0" | disc_mon == 3 & hmoind03 == "0" | disc_mon == 4 & hmoind04 == "0" | disc_mon == 5 & hmoind05 == "0" | disc_mon == 6 & hmoind06 == "0" | disc_mon == 7 & hmoind07 == "0" | disc_mon == 8 & hmoind08 == "0" | disc_mon == 9 & hmoind09 == "0" | disc_mon == 10 & hmoind10 == "0" | disc_mon == 11 & hmoind11 == "0" |  disc_mon == 12 & hmoind12 == "0" )
			drop disc_mon hmoind*

			gen adm_month = month(from_dt)

			di "merging in ccs code of dx1..."
			if `year' < 2015{
				// Assign diagnosis CCS
				rename icd_dgns_cd1 dx1 
				merge m:1 dx1 using "${DataPath}/SingleDXCCS_icd9.dta"
				tab _merge
				keep if _merge == 1 | _merge == 3
				drop _merge
				rename DXCCS dx1_ccs
			}

			if `year' >= 2000{
				// add in patient chronic conditions
				// merge in cc from current year
				di "merging in bsfcc from this year..."
		 		preserve
					use "${McareDataPath}/harm/100pct/bsfcc/`year'/bsfcc`year'.dta", clear
					// end of year variables
					foreach var of varlist ami alzh alzhdmta atrialfb cataract chrnkidn copd chf diabetes glaucoma ///
							hipfrac ischmcht depressn osteoprs ra_oa strketia cncrbrst cncrclrc cncrprst ///
							cncrlung cncrendm anemia asthma hyperl hyperp hypert hypoth {

							gen fl_`var'_end = cond(`var' == 1 | `var' == 3, 1, 0)
					}

					// mid year variables
					foreach var of varlist amim alzhm alzhdmtm atrialfm catarctm chrnkdnm copdm chfm diabtesm glaucmam ///
							hipfracm ischmchm deprssnm osteoprm ra_oa_m strktiam cncrbrsm cncrclrm cncrprsm ///
							cncrlngm cncendmm anemia_mid asthma_mid hyperl_mid hyperp_mid hypert_mid hypoth_mid{

							gen fl_`var'_mid = cond(`var' == 1 | `var' == 3, 1, 0)

					}

					egen n_cc_mid_curr = rowtotal(fl*mid)
					egen n_cc_end_curr = rowtotal(fl*end)

					gen atleast1_cc_mid_curr = cond(n_cc_mid > 0, 1, 0)
					gen atleast1_cc_end_curr = cond(n_cc_end > 0, 1, 0)
					gen atleast2_cc_mid_curr = cond(n_cc_mid > 1, 1, 0)
					gen atleast2_cc_end_curr = cond(n_cc_end > 1, 1, 0)

					rename fl_amim_mid 		fl_ami_mid
					rename fl_alzhm_mid 	fl_alzh_mid
					rename fl_alzhdmtm_mid fl_alzhdmta_mid
					rename fl_atrialfm_mid fl_atrialfb_mid
					rename fl_catarctm_mid fl_cataract_mid
					rename fl_chrnkdnm_mid fl_chrnkidn_mid
					rename fl_copdm_mid 	fl_copd_mid
					rename fl_chfm_mid		fl_chf_mid
					rename fl_diabtesm_mid	fl_diabetes_mid
					rename fl_glaucmam_mid fl_glaucoma_mid
					rename fl_hipfracm_mid fl_hipfrac_mid
					rename fl_ischmchm_mid fl_ischmcht_mid
					rename fl_deprssnm_mid fl_depressn_mid
					rename fl_osteoprm_mid fl_osteoprs_mid
					rename fl_ra_oa_m_mid 	fl_ra_oa_mid
					rename fl_strktiam_mid fl_strketia_mid
					rename fl_cncrbrsm_mid fl_cncrbrst_mid
					rename fl_cncrclrm_mid fl_cncrclrc_mid
					rename fl_cncrprsm_mid fl_cncrprst_mid
					rename fl_cncrlngm_mid fl_cncrlung_mid
					rename fl_cncendmm_mid fl_cncrendm_mid
					rename fl_anemia_mid_mid fl_anemia_mid
					rename fl_asthma_mid_mid fl_asthma_mid
					rename fl_hyperl_mid_mid fl_hyperl_mid
					rename fl_hyperp_mid_mid fl_hyperp_mid
					rename fl_hypert_mid_mid fl_hypert_mid
					rename fl_hypoth_mid_mid fl_hypoth_mid

					keep fl*_mid fl*_end n_* atleast* bene_id

					rename fl*_mid fl*_mid_curr
					rename fl*_end fl*_end_curr

					duplicates drop bene_id, force
					tempfile temp
					save `temp'
				restore

				merge m:1 bene_id using `temp', keep(1 3) nogen

				di "merging in bsfcc from last year..."
			 	// merge in cc from last year
			 	local lastyear = `year' - 1
			 		preserve
						use "${McareDataPath}/harm/100pct/bsfcc/`lastyear'/bsfcc`lastyear'.dta", clear
						// end of year variables
						foreach var of varlist ami alzh alzhdmta atrialfb cataract chrnkidn copd chf diabetes glaucoma ///
								hipfrac ischmcht depressn osteoprs ra_oa strketia cncrbrst cncrclrc cncrprst ///
								cncrlung cncrendm anemia asthma hyperl hyperp hypert hypoth {

								gen fl_`var'_end = cond(`var' == 1 | `var' == 3, 1, 0)
						}

						// mid year variables
						foreach var of varlist amim alzhm alzhdmtm atrialfm catarctm chrnkdnm copdm chfm diabtesm glaucmam ///
								hipfracm ischmchm deprssnm osteoprm ra_oa_m strktiam cncrbrsm cncrclrm cncrprsm ///
								cncrlngm cncendmm anemia_mid asthma_mid hyperl_mid hyperp_mid hypert_mid hypoth_mid{

								gen fl_`var'_mid = cond(`var' == 1 | `var' == 3, 1, 0)

						}

						egen n_cc_mid_last = rowtotal(fl*mid)
						egen n_cc_end_last = rowtotal(fl*end)

						gen atleast1_cc_mid_last = cond(n_cc_mid > 0, 1, 0)
						gen atleast1_cc_end_last = cond(n_cc_end > 0, 1, 0)
						gen atleast2_cc_mid_last = cond(n_cc_mid > 1, 1, 0)
						gen atleast2_cc_end_last = cond(n_cc_end > 1, 1, 0)

						rename fl_amim_mid 		fl_ami_mid
						rename fl_alzhm_mid 	fl_alzh_mid
						rename fl_alzhdmtm_mid fl_alzhdmta_mid
						rename fl_atrialfm_mid fl_atrialfb_mid
						rename fl_catarctm_mid fl_cataract_mid
						rename fl_chrnkdnm_mid fl_chrnkidn_mid
						rename fl_copdm_mid 	fl_copd_mid
						rename fl_chfm_mid		fl_chf_mid
						rename fl_diabtesm_mid	fl_diabetes_mid
						rename fl_glaucmam_mid fl_glaucoma_mid
						rename fl_hipfracm_mid fl_hipfrac_mid
						rename fl_ischmchm_mid fl_ischmcht_mid
						rename fl_deprssnm_mid fl_depressn_mid
						rename fl_osteoprm_mid fl_osteoprs_mid
						rename fl_ra_oa_m_mid 	fl_ra_oa_mid
						rename fl_strktiam_mid fl_strketia_mid
						rename fl_cncrbrsm_mid fl_cncrbrst_mid
						rename fl_cncrclrm_mid fl_cncrclrc_mid
						rename fl_cncrprsm_mid fl_cncrprst_mid
						rename fl_cncrlngm_mid fl_cncrlung_mid
						rename fl_cncendmm_mid fl_cncrendm_mid
						rename fl_anemia_mid_mid fl_anemia_mid
						rename fl_asthma_mid_mid fl_asthma_mid
						rename fl_hyperl_mid_mid fl_hyperl_mid
						rename fl_hyperp_mid_mid fl_hyperp_mid
						rename fl_hypert_mid_mid fl_hypert_mid
						rename fl_hypoth_mid_mid fl_hypoth_mid

						keep fl*_mid fl*_end n_* atleast* bene_id

						rename fl*_mid fl*_mid_last
						rename fl*_end fl*_end_last

						duplicates drop bene_id, force
						tempfile temp
						save `temp'
					restore

					merge m:1 bene_id using `temp', keep(1 3) nogen

				// assign relevant cc
				// assign using *admission* date
				// if admitted in first 6 months of year y, use end-of-year cc's from year y-1
				// if admitted in second 6 months of year y, use mid-year cc's from year y
					local vars "ami alzh alzhdmta atrialfb cataract chrnkidn copd chf diabetes glaucoma hipfrac ischmcht depressn osteoprs ra_oa strketia cncrbrst cncrclrc cncrprst cncrlung cncrendm anemia asthma hyperl hyperp hypert hypoth"
					foreach var of local vars{
						gen 	fl_`var' = fl_`var'_end_last if adm_month <= 6 			//accidentally added two __ in the earlier code after "fl"...
						replace fl_`var' = fl_`var'_mid_curr if adm_month >= 7

					}
					gen 	n_cc = n_cc_end_last if adm_month <= 6
					replace n_cc = n_cc_mid_last if adm_month >= 7

					gen 	atleast1_cc = atleast1_cc_end_last if adm_month <= 6
					replace atleast1_cc = atleast1_cc_mid_curr if adm_month >= 7
					drop fl*last fl*curr
			}
			

			save "${DataPath}/emergencyvisits/opemergencyvisits_`year'.dta", replace 
			
		}  // forval year


	*--------------------------------------------------
	* Adding in SUDGAP - outpatient
	*--------------------------------------------------

	forval year = 2012/2014{
		di "SUDGAP Outpatient - `year'"
		use clm_id bene_id rev_cntr using "${SUDDataPath}/harm/100pct/op/`year'/sudopr`year'.dta", clear
		gen nonnum = real(rev_cntr) == .
		tab nonnum
		drop if nonnum
		drop nonnum
		destring rev_cntr, replace

		gen ambulance_temp = rev_cntr >= 540 & rev_cntr <= 549
		bys clm_id: egen ambulance = max(ambulance_temp)

		keep if (rev_cntr >= 450 & rev_cntr <= 459) | rev_cntr == 981
		duplicates drop clm_id, force
		keep clm_id bene_id  ambulance 
		gen file_year = `year' 

		merge m:1 bene_id using "${McareDataPath}/harm/100pct/bsfab/`year'/bsfab`year'.dta", keepusing(bene_dob sex race g_bene_zip5 death_dt hmoind*)
		tab _merge 
		keep if _merge == 3 | _merge == 1
		drop _merge 	

		preserve
			use clm_id icd_dgns_cd1 from_dt thru_dt tot_chrg pmt_amt prvdrpmt using "${SUDDataPath}/harm/100pct/op/`year'/sudopc`year'.dta", clear
			keep if clm_id != ""
			*duplicates drop clm_id, force
			tempfile temp
			save `temp'
		restore

		merge m:1 clm_id using `temp'
		tab _merge 
		keep if _merge == 3 | _merge == 1
		drop _merge

		// Keep only if FFS patient in that month
		gen disc_mon = month(thru_dt)
		gen adm_month = month(from_dt)
		keep if (disc_mon == 1 & hmoind01 == "0" | disc_mon == 2 & hmoind02 == "0" | disc_mon == 3 & hmoind03 == "0" | disc_mon == 4 & hmoind04 == "0" | disc_mon == 5 & hmoind05 == "0" | disc_mon == 6 & hmoind06 == "0" | disc_mon == 7 & hmoind07 == "0" | disc_mon == 8 & hmoind08 == "0" | disc_mon == 9 & hmoind09 == "0" | disc_mon == 10 & hmoind10 == "0" | disc_mon == 11 & hmoind11 == "0" |  disc_mon == 12 & hmoind12 == "0" )
		drop disc_mon hmoind*

		// Assign diagnosis CCS
		rename icd_dgns_cd1 dx1 
		merge m:1 dx1 using "${DataPath}/SingleDXCCS_icd9.dta"
		tab _merge
		keep if _merge == 1 | _merge == 3
		drop _merge
		rename DXCCS dx1_ccs
		*save "${DataPath}/emergencyvisits/SUDopemergencyvisits_`year'.dta", replace

		local nobs = _N
		di "SUDGAP op obs = `nobs'"

		*use "${DataPath}/emergencyvisits/SUDopemergencyvisits_`year'.dta", clear
		// add in patient chronic conditions
			// merge in cc from current year
	 		preserve
				use "${McareDataPath}/harm/100pct/bsfcc/`year'/bsfcc`year'.dta", clear
				// end of year variables
				foreach var of varlist ami alzh alzhdmta atrialfb cataract chrnkidn copd chf diabetes glaucoma ///
						hipfrac ischmcht depressn osteoprs ra_oa strketia cncrbrst cncrclrc cncrprst ///
						cncrlung cncrendm anemia asthma hyperl hyperp hypert hypoth {

						gen fl_`var'_end = cond(`var' == 1 | `var' == 3, 1, 0)
				}

				// mid year variables
				foreach var of varlist amim alzhm alzhdmtm atrialfm catarctm chrnkdnm copdm chfm diabtesm glaucmam ///
						hipfracm ischmchm deprssnm osteoprm ra_oa_m strktiam cncrbrsm cncrclrm cncrprsm ///
						cncrlngm cncendmm anemia_mid asthma_mid hyperl_mid hyperp_mid hypert_mid hypoth_mid{

						gen fl_`var'_mid = cond(`var' == 1 | `var' == 3, 1, 0)

				}

				egen n_cc_mid_curr = rowtotal(fl*mid)
				egen n_cc_end_curr = rowtotal(fl*end)

				gen atleast1_cc_mid_curr = cond(n_cc_mid > 0, 1, 0)
				gen atleast1_cc_end_curr = cond(n_cc_end > 0, 1, 0)
				gen atleast2_cc_mid_curr = cond(n_cc_mid > 1, 1, 0)
				gen atleast2_cc_end_curr = cond(n_cc_end > 1, 1, 0)

				rename fl_amim_mid 		fl_ami_mid
				rename fl_alzhm_mid 	fl_alzh_mid
				rename fl_alzhdmtm_mid fl_alzhdmta_mid
				rename fl_atrialfm_mid fl_atrialfb_mid
				rename fl_catarctm_mid fl_cataract_mid
				rename fl_chrnkdnm_mid fl_chrnkidn_mid
				rename fl_copdm_mid 	fl_copd_mid
				rename fl_chfm_mid		fl_chf_mid
				rename fl_diabtesm_mid	fl_diabetes_mid
				rename fl_glaucmam_mid fl_glaucoma_mid
				rename fl_hipfracm_mid fl_hipfrac_mid
				rename fl_ischmchm_mid fl_ischmcht_mid
				rename fl_deprssnm_mid fl_depressn_mid
				rename fl_osteoprm_mid fl_osteoprs_mid
				rename fl_ra_oa_m_mid 	fl_ra_oa_mid
				rename fl_strktiam_mid fl_strketia_mid
				rename fl_cncrbrsm_mid fl_cncrbrst_mid
				rename fl_cncrclrm_mid fl_cncrclrc_mid
				rename fl_cncrprsm_mid fl_cncrprst_mid
				rename fl_cncrlngm_mid fl_cncrlung_mid
				rename fl_cncendmm_mid fl_cncrendm_mid
				rename fl_anemia_mid_mid fl_anemia_mid
				rename fl_asthma_mid_mid fl_asthma_mid
				rename fl_hyperl_mid_mid fl_hyperl_mid
				rename fl_hyperp_mid_mid fl_hyperp_mid
				rename fl_hypert_mid_mid fl_hypert_mid
				rename fl_hypoth_mid_mid fl_hypoth_mid

				keep fl*_mid fl*_end n_* atleast* bene_id

				rename fl*_mid fl*_mid_curr
				rename fl*_end fl*_end_curr

				duplicates drop bene_id, force
				tempfile temp
				save `temp'
			restore

			merge m:1 bene_id using `temp', keep(1 3) nogen

		 	// merge in cc from last year
		 	local lastyear = `year' - 1
		 		preserve
					use "${McareDataPath}/harm/100pct/bsfcc/`lastyear'/bsfcc`lastyear'.dta", clear
					// end of year variables
					foreach var of varlist ami alzh alzhdmta atrialfb cataract chrnkidn copd chf diabetes glaucoma ///
							hipfrac ischmcht depressn osteoprs ra_oa strketia cncrbrst cncrclrc cncrprst ///
							cncrlung cncrendm anemia asthma hyperl hyperp hypert hypoth {

							gen fl_`var'_end = cond(`var' == 1 | `var' == 3, 1, 0)
					}

					// mid year variables
					foreach var of varlist amim alzhm alzhdmtm atrialfm catarctm chrnkdnm copdm chfm diabtesm glaucmam ///
							hipfracm ischmchm deprssnm osteoprm ra_oa_m strktiam cncrbrsm cncrclrm cncrprsm ///
							cncrlngm cncendmm anemia_mid asthma_mid hyperl_mid hyperp_mid hypert_mid hypoth_mid{

							gen fl_`var'_mid = cond(`var' == 1 | `var' == 3, 1, 0)

					}

					egen n_cc_mid_last = rowtotal(fl*mid)
					egen n_cc_end_last = rowtotal(fl*end)

					gen atleast1_cc_mid_last = cond(n_cc_mid > 0, 1, 0)
					gen atleast1_cc_end_last = cond(n_cc_end > 0, 1, 0)
					gen atleast2_cc_mid_last = cond(n_cc_mid > 1, 1, 0)
					gen atleast2_cc_end_last = cond(n_cc_end > 1, 1, 0)

					rename fl_amim_mid 		fl_ami_mid
					rename fl_alzhm_mid 	fl_alzh_mid
					rename fl_alzhdmtm_mid fl_alzhdmta_mid
					rename fl_atrialfm_mid fl_atrialfb_mid
					rename fl_catarctm_mid fl_cataract_mid
					rename fl_chrnkdnm_mid fl_chrnkidn_mid
					rename fl_copdm_mid 	fl_copd_mid
					rename fl_chfm_mid		fl_chf_mid
					rename fl_diabtesm_mid	fl_diabetes_mid
					rename fl_glaucmam_mid fl_glaucoma_mid
					rename fl_hipfracm_mid fl_hipfrac_mid
					rename fl_ischmchm_mid fl_ischmcht_mid
					rename fl_deprssnm_mid fl_depressn_mid
					rename fl_osteoprm_mid fl_osteoprs_mid
					rename fl_ra_oa_m_mid 	fl_ra_oa_mid
					rename fl_strktiam_mid fl_strketia_mid
					rename fl_cncrbrsm_mid fl_cncrbrst_mid
					rename fl_cncrclrm_mid fl_cncrclrc_mid
					rename fl_cncrprsm_mid fl_cncrprst_mid
					rename fl_cncrlngm_mid fl_cncrlung_mid
					rename fl_cncendmm_mid fl_cncrendm_mid
					rename fl_anemia_mid_mid fl_anemia_mid
					rename fl_asthma_mid_mid fl_asthma_mid
					rename fl_hyperl_mid_mid fl_hyperl_mid
					rename fl_hyperp_mid_mid fl_hyperp_mid
					rename fl_hypert_mid_mid fl_hypert_mid
					rename fl_hypoth_mid_mid fl_hypoth_mid

					keep fl*_mid fl*_end n_* atleast* bene_id

					rename fl*_mid fl*_mid_last
					rename fl*_end fl*_end_last

					duplicates drop bene_id, force
					tempfile temp
					save `temp'
				restore

				merge m:1 bene_id using `temp', keep(1 3) nogen

			// assign relevant cc
			// assign using *admission* date
			// if admitted in first 6 months of year y, use end-of-year cc's from year y-1
			// if admitted in second 6 months of year y, use mid-year cc's from year y
				local vars "ami alzh alzhdmta atrialfb cataract chrnkidn copd chf diabetes glaucoma hipfrac ischmcht depressn osteoprs ra_oa strketia cncrbrst cncrclrc cncrprst cncrlung cncrendm anemia asthma hyperl hyperp hypert hypoth"
				foreach var of local vars{
					gen 	fl_`var' = fl_`var'_end_last if adm_month <= 6 			//accidentally added two __ in the earlier code after "fl"...
					replace fl_`var' = fl_`var'_mid_curr if adm_month >= 7

				}
				gen 	n_cc = n_cc_end_last if adm_month <= 6
				replace n_cc = n_cc_mid_last if adm_month >= 7

				gen 	atleast1_cc = atleast1_cc_end_last if adm_month <= 6
				replace atleast1_cc = atleast1_cc_mid_curr if adm_month >= 7
				drop fl*last fl*curr

		save "${DataPath}/emergencyvisits/SUDopemergencyvisits_`year'.dta", replace

	} //forval year
} // op_indivyear


*--------------------------------------------------
* Use previously-constructed outpatient ED visit file (identifyemergency.do), and compare to MEDPAR file.
* Eliminate outpatient ED visits that "overlap" with an inpatient stay, where overlap is defined as:
	* (1) OP followed by IP: 
		* 1a: if providers are the same: IP within 1 day of OP
		* 1b: if providers are different: IP within 0 days of OP
	* (2) IP followed by OP:
		* 2a: if providers are the same: OP within 0 days of IP
		* 2b: if providers are different: OP within < 0 days of IP

	* NOTE: if inpatient start date - ED visit end date == 1 and the provider is *different* across the two, I do count this as an outpatient ED visit for the 1st hospital and a separate inpatient stay for the second hospital. for now, do not count this as an overlap
*--------------------------------------------------
if `ipop_indivyear' == 1{
	forval year = 2006/2015{
		// Previously-constructed outpatient ED visit files
		use "${DataPath}/emergencyvisits/opemergencyvisits_`year'.dta", clear
		if `year' < 2015{
			keep clm_id bene_id prvdrnum dx1 prvdrpmt from_dt thru_dt pmt_amt tot_chrg bene_dob death_dt sex race g_bene_zip ambulance
		}

		if `year' >= 2015{
			keep clm_id bene_id prvdrnum prvdrpmt from_dt thru_dt pmt_amt tot_chrg bene_dob death_dt sex race g_bene_zip ambulance	
		}
		
		gen ip = 0

		// ALL inpatient stays
		preserve	
			use "${McareDataPath}/harm/100pct/med/`year'/med`year'.dta", clear
			// Generate (my own) claim ID
			gen clm_id_2 = _n
			tostring clm_id_2, replace
			replace clm_id_2 = clm_id_2 + " `year'"

			// Restrict to PPS claims

				* keep only short-stay claims
				display "short stay claims"
				tab sslssnf, missing
				keep if sslssnf=="S"
				
				* drop GHO (HMO) paid claims
				display "GHO paid claims"
				tab ghopdcd, missing
				drop if ghopdcd=="1"
				
				* keep only PPS claims
				display "PPS claims"
				tab pps_ind, missing
				keep if pps_ind=="2"
				
				* remove "IME only" claims
				display "IME only claims"
				count if abs(drgprice-ime_amt)<.01
				drop if abs(drgprice-ime_amt)<.01
				
				if (`year'>=2011) {
					* limit to FFS inpatient claims
					display "claim types"
					tab clm_type, missing
					keep if clm_type=="60"
				}
				
				* remove units within a hospital
				display "units within a hospital"
				tab spclunit, missing
				keep if spclunit=="" | spclunit==" "

				* identify ambulance
				di "identify ambulance charges"
				gen ambulance_temp = amblnc > 0 & !missing(amblnc)
				bys clm_id: egen ambulance = max(ambulance_temp)
				drop ambulance_temp


			keep clm_id_2 bene_id ad_dgns drg_cd admsndt dschrgdt prcdrcnt totchrg er_amt prvdrnum admsndt dschrgdt pmt_amt ambulance
			gen ip = 1

			tempfile temp
			save `temp'
		restore

		append using `temp'

		// Incorporate SUDGAP for 2012/2014
		if `year' >= 2012 & `year' <= 2014{
			// SUDGAP - outpatient (previously constructed emergency visit file)
			preserve
				use "${DataPath}/emergencyvisits/SUDopemergencyvisits_`year'.dta", clear
				gen ip = 0
				tempfile temp
				save `temp'
			restore

			append using `temp'

			// SUDGAP - inpatient
			preserve
				use "${SUDDataPath}/harm/100pct/med/`year'/sudmed`year'.dta", clear
				// Generate (my own) claim ID
				gen clm_id_2 = _n
				tostring clm_id_2, replace
				replace clm_id_2 = clm_id_2 + " `year'"

				// Restrict to PPS claims
					* keep only short-stay claims
					display "short stay claims"
					tab sslssnf, missing
					keep if sslssnf=="S"
					
					* drop GHO (HMO) paid claims
					display "GHO paid claims"
					tab ghopdcd, missing
					drop if ghopdcd=="1"
					
					* keep only PPS claims
					display "PPS claims"
					tab pps_ind, missing
					keep if pps_ind=="2"
					
					* remove "IME only" claims
					display "IME only claims"
					count if abs(drgprice-ime_amt)<.01
					drop if abs(drgprice-ime_amt)<.01
					
					if (`year'>=2011) {
						* limit to FFS inpatient claims
						display "claim types"
						tab clm_type, missing
						keep if clm_type=="60"
					}
					
					* remove units within a hospital
					display "units within a hospital"
					tab spclunit, missing
					keep if spclunit=="" | spclunit==" "

					* identify ambulance
					di "identify ambulance charges"
					gen ambulance_temp = amblnc > 0 & !missing(amblnc)
					bys clm_id: egen ambulance = max(ambulance_temp)
					drop ambulance_temp

					keep clm_id_2 bene_id ad_dgns drg_cd admsndt dschrgdt prcdrcnt totchrg er_amt prvdrnum admsndt dschrgdt pmt_amt ambulance
					gen ip = 1


					tempfile temp
					save `temp'
			restore

			append using `temp'
		}
		

		// Combine the start and end dates across the outpatient and MEDPAR files
		gen 	from_dt_comb = from_dt 
		replace from_dt_comb = admsndt if missing(from_dt)
		assert !missing(from_dt_comb)

		gen 	thru_dt_comb = thru_dt 
		replace thru_dt_comb = dschrgdt if missing(thru_dt)
		assert !missing(thru_dt_comb)

		format %td from_dt_comb thru_dt_comb

		sort bene_id from_dt_comb thru_dt_comb

		// Calculate difference between thru_dt of current visit and from_dt of next one
		bys bene_id (from_dt_comb thru_dt_comb): gen 	 days_overlap_next = from_dt_comb[_n+1] - thru_dt_comb[_n]
		bys bene_id (from_dt_comb thru_dt_comb): replace days_overlap_next = . if _n == _N
		gen overlap_next_neg = days_overlap_next < 0
		gen overlap_next_lt0 = days_overlap_next <= 0
		gen overlap_next_lt1 = days_overlap_next <= 1

		// Inpatient-outpatient pattern between this visit and next
		bys bene_id (from_dt_comb thru_dt_comb): gen 	 ip_op = ip[_n] == 1 & ip[_n+1] == 0
		bys bene_id (from_dt_comb thru_dt_comb): replace ip_op = . if _n == _N
		bys bene_id (from_dt_comb thru_dt_comb): gen 	 op_ip = ip[_n] == 0 & ip[_n+1] == 1
		bys bene_id (from_dt_comb thru_dt_comb): replace op_ip = . if _n == _N

		// Same provider from one visit to the next?
		bys bene_id (from_dt_comb thru_dt_comb): gen 	 same_pn = prvdrnum[_n] == prvdrnum[_n+1]
		bys bene_id (from_dt_comb thru_dt_comb): replace same_pn = . if _n == _N

		// Case 2: 96% of overlaps in 2010
		// Overlap Case 1a: OP followed by IP, providers are the same, and IP within 1 day of OP    -- 29% of overlaps in 2010
		gen overlap_1a = op_ip & same_pn & days_overlap_next <= 1
		bys bene_id (from_dt_comb thru_dt_comb): replace overlap_1a = . if _n == _N

		// Overlap Case 1b: OP followed by IP, providers are different, and IP within 0 days of OP  -- 67% of overlaps in 2010
		gen overlap_1b = op_ip & !same_pn & days_overlap_next <= 0
		bys bene_id (from_dt_comb thru_dt_comb): replace overlap_1b = . if _n == _N

		// Case 2: 4% of overlaps
		// Overlap Case 2a: IP followed by OP, providers are the same, and OP within 0 days of IP   -- 4% of overlaps
		gen overlap_2a = ip_op & same_pn & days_overlap_next <= 0
		bys bene_id (from_dt_comb thru_dt_comb): replace overlap_2a = . if _n == _N

		// Overlap Case 2b: IP followed by OP, providers are different, and OP within < 0 days of IP -- 0.15% of overlaps
		gen overlap_2b = ip_op & !same_pn & days_overlap_next < 0
		bys bene_id (from_dt_comb thru_dt_comb): replace overlap_2b = . if _n == _N


		// Combine 4 overlap cases												-- overlaps account for 3.8% of IP+OP claims
		gen overlap = overlap_1a | overlap_1b | overlap_2a | overlap_2b
		bys bene_id (from_dt_comb thru_dt_comb): replace overlap = . if  _n == _N
		bys bene_id: egen any_overlap = max(overlap)

		// When there is an overlap, do not count the OP claim -- drop_op == 1 for about 3% of outpatient claims in 2010
		gen drop_op = 0
		replace drop_op = 1 if overlap_1a == 1 | overlap_1b == 1 
		bys bene_id (from_dt_comb thru_dt_comb): replace drop_op = 1 if overlap_2a[_n-1] == 1  | overlap_2b[_n-1] == 1 

		assert ip == 0 if drop_op

		// Drop the outpatient claims
		drop if drop_op == 1
		drop drop_op

		// Drop the inpatient claims that do not have an emergency stay attached
		drop if (er_amt == 0 & ip == 1) | (missing(er_amt) & ip ==1)

		// Save yearly dataset of emergency visits 
		save "${DataPath}/emergencyvisits/emergencyvisits_ipop_nooverlap_`year'.dta", replace
	} // forval year
} //ipop_indivyear

*--------------------------------------------------
* Construct collapsed dataset of share of ED visits that are admitted, n of total visits, and n of total admissions from ED
*--------------------------------------------------
if `ipop_collapsed' == 1{
	use "${DataPath}/emergencyvisits/emergencyvisits_ipop_nooverlap_2006.dta", replace
	gen file_year = 2006
	append using "${DataPath}/emergencyvisits/emergencyvisits_ipop_nooverlap_2007.dta"
	replace file_year = 2007 if missing(file_year)

	// create fiscal year using discharge month
	gen disch_mon = month(thru_dt_comb)
	keep if (file_year == 2006 & disch_mon >= 10) | (file_year == 2007 & disch_mon < 10)

	gcollapse (mean) p_ip = ip (count) n_visits = ip (sum) n_ip = ip, by(prvdrnum)
	gen fyear = 2007

	forval year = 2008/2014{
		local year1 = `year' + 1
		preserve
			use "${DataPath}/emergencyvisits/emergencyvisits_ipop_nooverlap_`year'.dta", replace
			cap gen file_year = `year'
			append using "${DataPath}/emergencyvisits/emergencyvisits_ipop_nooverlap_`year1'.dta"
			cap replace file_year = `year1' if missing(file_year)

			// create fiscal year using discharge month
			gen disch_mon = month(thru_dt_comb)
			keep if (file_year == `year' & disch_mon >= 10) | (file_year == `year1' & disch_mon < 10)

			gcollapse (mean) p_ip = ip (count) n_visits = ip (sum) n_ip = ip, by(prvdrnum)
			gen fyear = `year1'

			tempfile temp
			save `temp'
		restore

		append using `temp'
	} //forval 2000/2015

	save "${DataPath}/emergencyvisits/n_ip_emergencyvisits_nooverlaps_fy_0715.dta", replace

} // ipop_collapsed

*--------------------------------------------------
* Summarize patient outcomes
*--------------------------------------------------
if `combine_ptoutcomes' == 1{
	use bene_id  prvdrnum death_dt ip from_dt thru_dt using "${DataPath}/emergencyvisits/emergencyvisits_ipop_nooverlap_2007.dta", replace
	gen file_year = 2007

	forval year = 2008/2015{
		di "appending `year'..."
		append using "${DataPath}/emergencyvisits/emergencyvisits_ipop_nooverlap_`year'.dta", keep(bene_id death_dt from_dt thru_dt prvdrnum ip)
		replace file_year = `year' if missing(file_year)
	}
	

	gen die_30d = death_dt - thru_dt <= 30
	gen die_60d = death_dt - thru_dt <= 60
	gen die_90d = death_dt - thru_dt <= 90



	bys bene_id (from_dt): gen next_visit_dt = from_dt[_n+1]

	gen revisit_30d = next_visit_dt - thru_dt <= 30
	gen revisit_60d = next_visit_dt - thru_dt <= 60
	gen revisit_90d = next_visit_dt - thru_dt <= 90

	local days "30 60 90"
	foreach nday of local days{
		gen in_die_`nday'd = die_`nday'd & ip == 1
		gen out_die_`nday'd_ = die_`nday'd & ip == 0

		gen in_revisit_`nday'd = revisit_`nday'd & ip == 1
		gen out_revisit_`nday'd = revisit_`nday'd & ip == 0
	}

	save "${DataPath}/emergencyvisits/claimlvl_ptoutcomes_emergencyvisits_nooverlaps_y_0715.dta", replace
}

*--------------------------------------------------
* Summarize patient outcomes by fiscal year
*--------------------------------------------------
if `ptoutcomes_fy' == 1{

	use "${DataPath}/emergencyvisits/claimlvl_ptoutcomes_emergencyvisits_nooverlaps_y_0715.dta", clear
	// create fiscal year using discharge month
	gen disch_mon = month(thru_dt)

	gen 	fyear = file_year
	replace fyear = file_year + 1 if disch_mon >= 10

	drop if fyear == 2006 | fyear == 2016

	gcollapse (mean) die_30d die_60d die_90d revisit_30d revisit_60d revisit_90d , by(prvdrnum fyear)



	save "${DataPath}/emergencyvisits/ptoutcomes_emergencyvisits_nooverlaps_fy_0715.dta", replace

} // ptoutcomes



log close
